<?php

namespace App\Models\CacheModel\ai_chat_room;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class AiChatFirstTxt extends BaseCache
{
    protected $table = 'ai_chat_room.ai_chat_first_txt';

    protected $cacheModel = 'ai_chat_room.chat_first_txt';

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        // 获取未删除 的数据
        $where = ['id' => ['in', $ids]];
        try
        {
            $data = $this->where($where)->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }


    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'type', 'option10', 'option20', 'option30', 'sex_type', 'create_time']);
        }

        return $data;
    }

    /**
     * 获取聊天首问IDS
     * @param int $option10
     * @param int $option20
     * @param int $option30
     * @param int $sexType
     * @return array|mixed
     * @throws \Exception
     */
    public function getFirstTxtIds(int $option10, int $option20, int $option30, int $sexType)
    {
        $key = sprintf('%d_%d_%d_%d', $option10, $option20, $option30, $sexType);

        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('first_txt_ids', $key)
            ->setCacheType('string')->getDataAndSet(function () use ($option10, $option20, $option30, $sexType)
            {
                try
                {
                    $ids = $this->where(['option10' => $option10, 'option20' => $option20, 'option30' => $option30, 'sex_type' => $sexType,])->column('id');

                    $idsStr = json_encode($ids, JSON_UNESCAPED_UNICODE);

                    return $idsStr;
                }
                catch (\Exception $e)
                {
                    \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                    return [];
                }
            }, [], 'first_txt_ids');

        return $resData ? json_decode($resData, true) : [];
    }

    /**
     * 随机获取聊天首问
     * @param array $configData
     * @return string
     * @throws \Exception
     */
    public function randFirstTxt(array $configData)
    {
        $option10 = $configData['option10']['ids'][0] ?? 0;
        $option20 = $configData['option20']['ids'][0] ?? 0;
        $option30 = $configData['option30']['ids'][0] ?? 0;
        $sexType = $configData['sex_type']['ids'][0] ?? 0;

        $ids = $this->getFirstTxtIds($option10, $option20, $option30, $sexType);
        if (empty($ids))
        {
            return '';
        }

        $randId = $ids[array_rand($ids)];

        $data = $this->getInfoById($randId);

        return $data['content'] ?? '';
    }
}
